<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class PurchaseToMaterial extends BaseModel
{
    protected $fillable = [
        'purchase_id',
        'material_id',
        'supplier_id',
        'number',
        'unit_price',
        'description',
        'expiration_date',
        'manufacture_date',
        'stock',
        'sell',
        'status'
    ];
    
    // record_materials
    public function recordmaterial()
    {
        return $this->hasOne(RecordMaterial::class);
    }

    public function purchase()
    {
        return $this->belongsTo(Purchase::class, 'purchase_id');
    }

    public function material()
    {
        return $this->belongsTo('App\Models\Material');
    }

    public function supplier()
    {
        return $this->belongsTo('App\Models\Supplier');
    }

    // 价格修改器，获取器
    public function setUnitPriceAttribute($value)
    {
        $this->attributes['unit_price'] = $value * 100;
    }

    public function getUnitPriceAttribute($value)
    {
        return $value / 100;
    }
    
    public static function getPurchases($materials_ids = [], $sortByDesc = false,int $store_id)
    {
		$materials = PurchaseToMaterial::whereHas('purchase',function (Builder $query) use($store_id) {
			            $query->where('store_id', $store_id)
				            ->where('status', 2);
			        })->whereIn('material_id', $materials_ids)
				        ->where('status', 1)
					->orderBy('material_id')
					->get();
		
		if($sortByDesc){
			$stock = $materials->groupBy('material_id')->map(function ($item) {
                $item->sortByDesc('created_at');
                return $item[0];
            })->toArray();
		} else{
			$stock = $materials->groupBy('material_id')->toArray();
		}
		return $stock;
    }
    
    // 批量更新
    public static function updateBatch($tableName = "", $multipleData = array())
    {
        if( $tableName && !empty($multipleData) ) {
 
            // column or fields to update
            $updateColumn = array_keys($multipleData[0]);
            $referenceColumn = $updateColumn[0]; //e.g id
            unset($updateColumn[0]);
            $whereIn = "";
 
            $q = "UPDATE ".$tableName." SET ";
            foreach ( $updateColumn as $uColumn ) {
                $q .=  $uColumn." = CASE ";
 
                foreach( $multipleData as $data ) {
                    $q .= "WHEN ".$referenceColumn." = ".$data[$referenceColumn]." THEN '".$data[$uColumn]."' ";
                }
                $q .= "ELSE ".$uColumn." END, ";
            }
            foreach( $multipleData as $data ) {
                $whereIn .= "'".$data[$referenceColumn]."', ";
            }
            $q = rtrim($q, ", ")." WHERE ".$referenceColumn." IN (".  rtrim($whereIn, ', ').")";

            // Update
            return DB::update(DB::raw($q));
 
        } else {
            return false;
        }
 
    }
}
